#####################################################################
# File:         HTMLHeaderFooter.py
# Product:      KnowledgeEngine
#
# Copyright:    Copyright (c) 2002, Faulkner Technologies.
# Author:       Brian Oliver
#
# Description:  This file defines the HTMLHeaderFooter class, a simple
#               Element that may be used to define a page header or 
#               footer in HTML
#
# See Also:     Element
#####################################################################

#####################################################################
## Knowledge Engine Library Imports
from Products.KnowledgeEngine.Common import *
from Products.KnowledgeEngine.KnowledgeEngineCore import *
from Products.KnowledgeEngine.Element import Element, ElementInfo
from Products.KnowledgeEngine.PropertyDefinition import PropertyDefinition
from Products.KnowledgeEngine.PropertyFormats import *
from Products.KnowledgeEngine.PropertyTypes import *

#####################################################################
## Zope Library Imports
from Globals import DTMLFile
from ImageFile import ImageFile

#####################################################################
## Python Library Imports
# (none)

#####################################################################
## HTMLHeaderFooterInfo Class Definition
class HTMLHeaderFooterInfo(ElementInfo):
    """
        Defines the class info methods, defining information about
        the class without needing an instance of the class
    """
    
    #---------------------------------------------------------------#
    
    def getClassName(self):
        """
            Returns the name of the class for which 
            we are providing information.
        """
        return "HTMLHeaderFooter"

    #---------------------------------------------------------------#

    def getDisplayName(self):
        """
            Return a friendly name for the class instances
            (this is used for displaying in lists etc)
        """
    
        return "HTML Header/Footer"
        
    #---------------------------------------------------------------#

    def getIdentityType(self):
        """
            Return the identity type.  
        """
        
        return "headfoot"
        
    #---------------------------------------------------------------#
    
    def getClassIcon(self):
        """
            Return the appropriate icon file for the class.
        """
        
        return ImageFile('icon.gif', globals())
        
    #---------------------------------------------------------------#

    def isPermittedInContainer(self, containerClassInfo, containerInstance = None, instance=None):
        """
            ABSTRACT: Returns if class instances (instance) is permitted in 
            the specified container (containerInstance)
            
            NOTE: This is from this class' instance perspective.
        """
        
        #DEFAULT RULE: HTMLHeaderFooters are permitted in any GenericContainer
        return containerClassInfo.isInstanceOf('GenericContainer')
    
    #---------------------------------------------------------------#

    def onConfigureProperties(self):
        """
            Automatically called by the Knowledge Engine once
            the class has been loaded.  Use this method to register 
            class specific properties using the PropertyReflection interface.
            
            If this class is sub-classed, a call must be made to
            the super-class to ensure super-class properties are
            correctly registered
            
            NOTE: This method is called before ClassInfo.onAfterStartup()            
        """

        #initialise the super-class properties for reflection
        ElementInfo.onConfigureProperties(self)
      
        #add the 'headerContent' property        
        self.addPropertyDefinition(PropertyDefinition(identity="headerContent", displayName="Content", description="The content for the Header/Footer", propertyFormat=pf_HTML, defaultValue=""))

        #add the 'displayOption' property        
        self.addPropertyDefinition(PropertyDefinition(identity="displayOption", displayName="Content Location", description="The position of the content", propertyType=pt_STRING, propertyFormat=pf_NATIVE, hasEnumeratedValues=true, enumeratedValuesList=['Header','Footer'], defaultValue="Header"))

    #---------------------------------------------------------------#

#####################################################################
## HTMLHeaderFooter Class Definition
class HTMLHeaderFooter(Element):
    """
        A class to represent a simple report that uses the components
        within the containing Model to build a report
    """

    #---------------------------------------------------------------#

    #ABSTRACT TRANSIENT: a ClassInfo implementation instance to provide info about this class
    _CLASSINFO = HTMLHeaderFooterInfo()

    #---------------------------------------------------------------#

    # Custom zope manage_preview to permit previews of components.
    preview_html = DTMLFile('index_html', globals())

    #---------------------------------------------------------------#

    DISPLAY_FORMAT_OPTIONS = ['Header','Footer']

    #DEFAULT INSTANCE VALUES
    displayOption = 'Header'

    #---------------------------------------------------------------#

    def __init__(self, id, identity):
        "Constructor for the HTMLHeaderFooter"        

        Element.__init__(self, id, identity)
        
        self.headerContent = ""
        self.displayOption = "Header"
        
        #perform onAfterInstanciation processing
        self.onAfterInstanciation()

    #---------------------------------------------------------------#

    def onAfterInstanciation(self):
        """
            Handle Knowledge Engine specific actions after this object 
            has been loaded into memory.  
            
            This is useful for performing backwards compatibility
        """        

        #does the element have the legacy isHeader property?
        if hasattr(self, "isHeader"):
            #upgrade to the displayOption property
            if self.isHeader:
                self.displayOption = 'Header'
            else:
                self.displayOption = 'Footer'
            
            #remove the isHeader property
            delattr(self, "isHeader")
        
        #perform default processing
        Element.onAfterInstanciation(self)
        
    #---------------------------------------------------------------#

    def onAfterPropertyChange(self, propertyName, oldValue, newValue):
        "Handle when an object property has changed (using the property editor)"

        #has the header/footer property changed?
        if propertyName == "displayOption":
            self.updateDisplayPositionInModels()
            
        #perform default processing
        Element.onAfterPropertyChange(self, propertyName, oldValue, newValue)
                
    #---------------------------------------------------------------#
                
    def getIsHeader(self):
        """
            Return if this header/footer element is a header
        """
        
        return self.displayOption == 'Header'
                        
    #---------------------------------------------------------------#
                
    def getIsFooter(self):
        """
            Return if this header/footer element is a footer
        """
        
        return not self.getIsHeader()
                
    #---------------------------------------------------------------#

    def isDisplayedBeforeComponents(self):
        """
            Returns if the Element is to be displayed before
            components in a model. Alternatively the Element is
            displayed after the components in a model.
            (If the element is in a model)
        """
        
        #Headers are displayed before 
        return self.getIsHeader()
        
    #---------------------------------------------------------------#

    def getContent(self, contentZone, forGenericObject = None, componentContext = None, assessment = None):
        """
            Return the Element generated 
            content for the specified contentZone (a string).   

            This method is called by the Model.index_html dtml file
            to customise the appearance of the output.
            
            By overriding this method, Elements have the opportunity
            to perform customization of the output from a Model
            during the assessment process.            
        """

        if (self.getIsHeader() and contentZone == "PAGE_HEADER") or (self.getIsFooter() and contentZone == "PAGE_FOOTER"):
            return self.headerContent
            
        else:
            return ""
        
    #---------------------------------------------------------------#

    def isPreviewPermitted(self):
        """
            Return if the Element is allowed to be previewed.
            
            By default all elements can't be previewed, some however
            should can be previewed. eg: HTMLHeaderFooters
        """
        
        return true
        
    #---------------------------------------------------------------#

#####################################################################
## Class Initialisation

# Register Class Information with the Knowledge Engine
registerGenericObjectClass(HTMLHeaderFooter)

#####################################################################
